<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.Map.Entry,java.util.*,java.util.regex.*,java.net.*,java.io.*,java.text.*"%>
<%@ page import="com.tianxiabuyi.*,com.tianxiabuyi.persist.*,com.tianxiabuyi.model.*,com.tianxiabuyi.utils.*"%>
<%@ page import="org.jsoup.Jsoup,org.jsoup.nodes.Element,org.jsoup.select.Elements"%>
<%@ page import="org.dom4j.*"%>
<%@ page import="com.tianxiabuyi.model.pojo.Comment" %>
<%!
	public Integer QUEST_COMMENT = 1;  // 问答
	public Integer NEWS_COMMENT = 2;   // 新闻
	public Integer DOCTOR_COMMENT = 3; // 医生
	public Float score = 0f;
	// 评论
	String reply(Integer uid, Input input, Integer category) {
		Message msg = new Message(0);
		Integer srcId = input.getInt("id", 0);
		String content = input.getDesc("content", "");
		Integer hospital = input.getInt("hospital", -1);
		Float score = input.getFloat("score", 0f);
		String aid = input.getDesc("aids", "");
		if (content.length() < 3) {
			msg = new Message(30042, "输入的长度不合法");
			return msg.toJson();
		}
		String hql = "";
		if (category.equals(NEWS_COMMENT)) {
			hql = "from News where id = " + srcId;
		} else if (category.equals(QUEST_COMMENT)) {
			hql = "from Quest where id = " + srcId;
		} else if (category.equals(DOCTOR_COMMENT)) {
			hql = "from User where id = " + srcId;
		} else {
			msg = new Message(30041, "没有相关数据");
			return msg.toJson();
		}
		List srcList = QBuilder.db("basedb").queryHql(hql);
		if (srcList.size() == 0) {
			msg = new Message(30041, "没有相关数据");
			return msg.toJson();
		}
		Comment comment = new Comment();
		comment.setHospital(hospital);
		comment.setUid(uid);
		if (!"".equals(aid)) {
			comment.setJson("at", aid);
		}
		comment.setSrcId(srcId);
		comment.setContent(content);
		comment.setCategory(category);
		comment.setScore(score);
		String imgs = input.getDesc("imgs", "");
		comment.setImgs(JSONUtil.toJson(stringToList(imgs)));
		DBTools.db("basedb").addItem(comment);
		return msg.toJson();
	}

	// 查询评论
	public List<Map> query(Integer uid, Integer srcId, Integer hospital, Integer category) {
		Where where = new Where();
		if (uid != null && uid > 0) {
			where.and("uid=", uid);
		}
		List<Map> commentList = QBuilder.db("basedb").select("new map(id as id,case when imgs is null then '[]' else imgs end as imgs, uid as uid, content as content, cTime as create_time, jsonString as json, score as score)").from("Comment").where(where.and("srcId=", srcId).and("hospital=", hospital).and("category=", category)).order("id", "asc").query().getResultObj();
		List<Integer> uidList = new ArrayList<Integer>();
		float scores = 0f;
		for (int i = 0; i < commentList.size(); i++) {
			Map comment = commentList.get(i);
			scores += Tools.getFloat(comment.get("score") + "", 0f);
			uid = Tools.getInt(comment.get("uid") + "", 0);
			Long createTime = Tools.getLong(comment.get("create_time") + "");
			String create_time = DateUtil.formatDateByFormat(new Date(createTime), DateUtil.DATETIME_FORMAT);
			comment.put("create_time", create_time);
			String _uids = JSONUtil.fromJson(Tools.getDesc(comment.get("json")), new TypeReference<Map>(){}).get("at") + "";
			String[] strs = _uids.split("_");
			for (String str : strs) {
				uidList.add(Tools.getInt(str, -1));
			}
			uidList.add(uid);
		}
		score = scores / (commentList.size()==0 ? 1:commentList.size());
		Map<Integer, User> userMap = usersByUids(uidList);
		for (int i = 0; i < commentList.size(); i++) {
			Map comment = (Map)commentList.get(i);
			uid = Tools.getInt(comment.remove("uid") + "", 0);
			User user = userMap.get(uid);
			if (user != null) {
				String userName = user.getUserName();
				String nickName = user.getNickName();
				String avatar = user.getAvatar();
				String name = user.getName();
				comment.put("user_name", isEmpty(nickName) ? userName : nickName);
				comment.put("uid", uid);
				comment.put("avatar", avatar);
				comment.put("type", user.getType());  // 用于判断回答用户的角色
			}
			String _uids = JSONUtil.fromJson(Tools.getDesc(comment.remove("json")), new TypeReference<Map>(){}).get("at") + "";
			String[] strs = _uids.split("_");
			List<Map> users = new ArrayList<Map>();
			for (String str : strs) {
				uid = Tools.getInt(str, 0);
				user = userMap.get(uid);
				if (user != null) {
					Map _map = new HashMap();
					String userName = user.getUserName();
					String nickName = user.getNickName();
					String avatar = user.getAvatar();
					String name = user.getName();
					_map.put("user_name", isEmpty(nickName) ? userName : nickName);
					_map.put("uid", uid);
					_map.put("avatar", avatar);
					_map.put("type", user.getType());  // 用于判断回答用户的角色
					users.add(_map);
				}
			}
			comment.put("at", users);
		}
		return commentList;
	}
%>
